package com.gable.exhibition.service.impl;

import com.gable.common.util.ReturnResultUtil;
import com.gable.common.util.UUIDGenerator;
import com.gable.exhibition.dao.CommentInfoDao;
import com.gable.exhibition.service.CommentInfoService;
import com.gable.exhibition.util.MessageUtil;
import com.gable.exhibition.view.CommentInfoView;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by SJX on 2018/3/7.
 */
@Service("commentInfoService")
public class CommentInfoServiceImpl implements CommentInfoService {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    CommentInfoDao commentInfoDao;

    @Override
    public ReturnResultUtil queryCommentInfo(CommentInfoView commentInfoView) {
        List<CommentInfoView> list = commentInfoDao.queryCommentInfo(commentInfoView);
        if(list!=null && !list.isEmpty()){
            CommentInfoView cv = list.get(1);
            return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,cv);
        }
        return new ReturnResultUtil(200, MessageUtil.QUERYSUCCESS,new CommentInfoView());
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ReturnResultUtil addCommentInfo(CommentInfoView commentInfoView) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        commentInfoView.setCommentInfoId(UUIDGenerator.getUniqueLong());
        commentInfoView.setCreateTime(df.format(new Date()));
        commentInfoView.setIsDel(0);
        commentInfoDao.insertSelective(commentInfoView);
        return new ReturnResultUtil(200, MessageUtil.SAVESUCCESS,commentInfoView);
    }
}
